package com.jerry.huihome.module.find.fragment;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseFragment;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.find.adapter.AssessFroemanAdapter;
import com.jerry.huihome.module.find.entity.AssessForeman;
import com.jerry.huihome.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/13.
 */
public class FroemanWordFragment extends BaseFragment {

    @BindView(R.id.tv_level_score)
    TextView tvLevelScore;
    @BindView(R.id.tv_level_average)
    TextView tvLevelAverage;
    @BindView(R.id.tv_attitude_score)
    TextView tvAttitudeScore;
    @BindView(R.id.tv_attitude_average)
    TextView tvAttitudeAverage;
    @BindView(R.id.tv_active_score)
    TextView tvActiveScore;
    @BindView(R.id.tv_active_average)
    TextView tvActiveAverage;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.list_foreman_word)
    PullToRefreshListView listForemanWord;
    private String pmid;
    private String url;
    private AssessForeman assessForeman;
    private List<AssessForeman.BodyBean.AssessItemsBean> list = new ArrayList<>();
    private AssessFroemanAdapter adapter;
    private int pageIndex = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.check_wrod_of_mouth;
    }

    @Override
    protected void getReqParams(Bundle bundle) {
        pmid = bundle.getString("pmid");
    }

    @Override
    protected void loadData() {
        url = Urls.FOREMAN_ASSESS_URL + pmid+"&pageIndex="+pageIndex;
        OkHttpUtils.get(url, new OkHttpUtils.OnGetDataListener() {
            @Override
            public void onResponse(String url, String data) {
                if (data != null) {
                    if (pageIndex == 1){
                        if (adapter != null){
                            adapter.clear();
                        }
                    }
                    Gson gson = new Gson();
                    assessForeman = gson.fromJson(data, AssessForeman.class);
                    setViews();
                }
            }
            @Override
            public void onFailure(String url, String error) {

            }
        });
    }

    private void setViews() {
        tvLevelScore.setText(assessForeman.getBody().getScoreProf() + "");
        tvAttitudeScore.setText(assessForeman.getBody().getScoreManner() + "");
        tvActiveScore.setText(assessForeman.getBody().getScoreActive() + "");
        double scoreProfDeta = assessForeman.getBody().getScoreProfDeta();
        if (scoreProfDeta > 0) {
            tvLevelAverage.setText("高于平均" + (int) (scoreProfDeta * 100)+"%");
        }
        if (scoreProfDeta < 0) {
            tvLevelAverage.setText("低于平均" + Math.abs((int) (scoreProfDeta * 100))+"%");
        } else {
            tvLevelAverage.setText("与平均持平");
        }
        double scoreMannerDeta = assessForeman.getBody().getScoreMannerDeta();
        if (scoreMannerDeta > 0) {
            tvAttitudeAverage.setText("高于平均" + (int) (scoreMannerDeta * 100)+"%");
        }
        if (scoreMannerDeta < 0) {
            tvAttitudeAverage.setText("低于平均" + Math.abs((int) (scoreMannerDeta * 100))+"%");
        } else {
            tvAttitudeAverage.setText("与平均持平");
        }
        Float scoreActiveDeta = assessForeman.getBody().getScoreActiveDeta();
        if (scoreActiveDeta > 0) {
            tvActiveAverage.setText("高于平均" + (int) (scoreActiveDeta * 100)+"%");
        }
        if (scoreActiveDeta < 0) {
            tvActiveAverage.setText("低于平均" + Math.abs((int) (scoreActiveDeta * 100))+"%");
        } else {
            tvActiveAverage.setText("与平均持平");
        }
        tvComment.setText("评论" + assessForeman.getBody().getAssessCount());
        List<AssessForeman.BodyBean.AssessItemsBean> assessItems = assessForeman.getBody().getAssessItems();
        if (assessItems == null){
            Toast.makeText(getContext(),"没有更多内容了",Toast.LENGTH_LONG).show();
        }else {
            list.addAll(assessItems);
            showAdapter();
        }
    }

    private void showAdapter() {
        adapter = new AssessFroemanAdapter(getContext(),list);
        listForemanWord.setAdapter(adapter);
        listForemanWord.getRefreshableView().setDivider(null);
        listForemanWord.getRefreshableView().setSelector(android.R.color.transparent);
        listForemanWord.setMode(PullToRefreshBase.Mode.BOTH);
        listForemanWord.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
        listForemanWord.onRefreshComplete();
    }

}
